//
//  HSRegisterVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/31.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSRegisterVC.h"
#import "HSProtocolVC.h"

@interface HSRegisterVC ()

@property (nonatomic, strong) UITextField *accountTF;
@property (nonatomic, strong) UITextField *pswTF;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, assign) BOOL isSend;

@end

@implementation HSRegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册";
    
    self.accountTF = [self fieldWithFrame:CGRectMake(30, 100, ScreenWidth - 60, 50) title:@"手机号码" placeholder:@"请输入手机号码"];
    self.accountTF.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.accountTF];
    self.pswTF = [self fieldWithFrame:CGRectMake(self.accountTF.left, self.accountTF.bottom + 25, self.accountTF.width, self.accountTF.height) title:@"验证码" placeholder:@"请输入验证码"];
    UIButton *button = [UIButton buttonWithFrame:CGRectMake(0, 0, 60, self.pswTF.height) font:MYFont(Font28) color:HEXCOLOR(HSColorRed) title:@"点击发送"];
    [button addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
    self.pswTF.rightView = button;
    self.pswTF.rightViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.pswTF];
    self.confirmBtn = [UIButton buttonWithFrame:CGRectMake(self.accountTF.left, self.pswTF.bottom + 50, self.accountTF.width, 40) font:MYFont(Font32) color:WHITE_COLOR title:@"注册"];
    self.confirmBtn.layer.cornerRadius = 10;
    self.confirmBtn.backgroundColor = HEXCOLOR(HSColorRed);
    [self.confirmBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    
    UIButton *protocol = [UIButton buttonWithFrame:CGRectMake(self.confirmBtn.right - 100, self.confirmBtn.bottom + 10, 100, 30) font:MYFont(Font26) color:HEXCOLOR(HSColorGray) title:@"用户协议"];
    [protocol addTarget:self action:@selector(protocolClick) forControlEvents:UIControlEventTouchUpInside];
    protocol.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.view addSubview:protocol];
    
    @weakify(self);
    [self.view bk_whenTapped:^{
        @strongify(self);
        [self.view endEditing:YES];
    }];
}

- (UITextField *)fieldWithFrame:(CGRect)frame title:(NSString *)title placeholder:(NSString *)placeholder
{
    UITextField *field = [[UITextField alloc] initWithFrame:frame];
    field.font = MYFont(Font30);
    field.textColor = HEXCOLOR(HSColorBlack);
    field.placeholder = placeholder;
    UILabel *left = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, field.height)];
    left.text = title;
    left.font = MYFont(Font32);
    //    left.backgroundColor = BLACK_COLOR;
    left.textColor = HEXCOLOR(HSColorGray);
    field.leftView = left;
    field.leftViewMode = UITextFieldViewModeAlways;
    [field showLineForDirection:MYDirectionBottom];
    return field;
}

- (void)registerClick
{
    if (self.accountTF.text.length != 11) {
        [UIWindow showFailedMsg:@"手机有误"];
        return;
    }
    if (self.pswTF.text.length < 1) {
        [UIWindow showWarnMsg:@"验证码不能为空"];
    }
    else {
        [UIWindow showFailedMsg:@"验证码有误"];
    }
}

- (void)sendClick
{
    if (self.accountTF.text.length != 11) {
        [UIWindow showFailedMsg:@"手机有误"];
        return;
    }
    if (![MYAppHelper shareHelper].hasNetWork) {
        [UIWindow showFailedMsg:@"请检查网络"];
        return;
    }
    if (self.isSend) {
        [UIWindow showFailedMsg:@"验证码已经发送过了"];
        return;
    }
    [UIWindow showProgress];
    @weakify(self);
    [[GCDQueue mainQueue] queueBlock:^{
        [UIWindow showSuccessMsg:@"验证码发送成功"];
        @strongify(self);
        self.isSend = YES;
    } afterDelay:2];
}

- (void)protocolClick
{
    [self my_push:[HSProtocolVC new]];
}

@end
